require(['config'], function () {
    require(['jquery'], function () {
        require(['jqcookie'], function () {
            const phpurl = 'http://10.31.155.19/MallProject/php/';
            console.log($.cookie().cookienum);

            let datatype = $.cookie('cookiedata').split(',');
            let sid = $.cookie('cookiesid').split(',');
            let num = $.cookie('cookienum').split(',');
            let goodslist = $('.cart-list');
            let nun = $('.all-nun');
            let select = $('.select');
            let allprice = $('.all-price');
            let allcheck = $('.cart-box .cart-bot .all-check .all-select');
            let strhtml = '';
            let sum = 0;
            let price = 0;

            $.when($.ajax({
                url: phpurl + 'falshsale.php',
                dataType: 'json'
            }), $.ajax({
                url: phpurl + 'hotsale.php',
                dataType: 'json'
            }), $.ajax({
                url: phpurl + 'f1.php',
                dataType: 'json'
            })).done(function (fsaledata, hsaledata, f1data) {
                console.log(fsaledata[0][sid[0]]);
                for (let j = 0; j < datatype.length; j++) {
                    sum += Number(num[j]);
                    //通过cookiedata的数据判断是从哪个表渲染，从而取得渲染数据
                    if (datatype[j] = 'falshsale') {
                        strhtml += `
                       <li class="hover1">
                       <div class="top f-cb">
                           <div class="fl check" data-value="2574" data-name="pid[]" data-itemtype="" data-price="${fsaledata[0][sid[j] - 1].price}">
                               <input type="checkbox" value="2574" name="pid[]"  itemtype="" data-price="">
                           </div>
                           <div class="fl intro">
                               <div class="pic">
                                    <img src="${fsaledata[0][sid[j] - 1].url}" alt="">
                                </div>
                                <h2>${fsaledata[0][sid[j] - 1].title}</h2>
                           </div>
                           <div class="fl price" data-unit="${fsaledata[0][sid[j] - 1].price}">¥<span class="unit-html simprice">${fsaledata[0][sid[j] - 1].price}</span></div>
                           <div class="fl numbox" data-pdtid="2574" data-type="0">
                               <span class="fl butn cut">-</span>
                               <span class="fl num">${num[j]}</span>
                               <span class="fl butn add">+</span>
                               <!-- 数量input -->
                               <input class="num-inp" type="hidden" value="1" id="pdt_num_2574" types="3"
                                   pdt_id="2574" stock="994" good_type="0" pdt_sale_price="${fsaledata[0][sid[j] - 1].price}">
                           </div>
                           <!--赠送积分-->
                           <div class="fl integral integral-pice" data-point="${fsaledata[0][sid[j] - 1].price}">${fsaledata[0][sid[j] - 1].price}</div>
                           <div class="fl plan">¥<span class="unit-html sum-pice">${Number(fsaledata[0][sid[j] - 1].price) * Number(num[j])}</span></div>
                           <div class="fl dele" data-pid="2574">删除</div>
                       </div>
                   </li>
                       `;
                    } else if (datatype[j] = 'hotsale') {
                        strhtml += `
                        <li class="hover1">
                        <div class="top f-cb">
                            <div class="fl check checked" data-value="2574" data-name="pid[]" data-itemtype="" data-price="${hsaledata[0][sid[j] - 1].price}">
                                <input type="checkbox" value="2574" name="pid[]" checked="" itemtype="" data-price="">
                            </div>
                            <div class="fl intro">
                                <div class="pic">
                                     <img src="${hsaledata[0][sid[j] - 1].url}" alt="">
                                 </div>
                                 <h2>${hsaledata[0][sid[j] - 1].title}</h2>
                            </div>
                            <div class="fl price" data-unit="${hsaledata[0][sid[j] - 1].price}">¥<span class="unit-html">${hsaledata[0][sid[j] - 1].price}</span></div>
                            <div class="fl numbox" data-pdtid="2574" data-type="0">
                                <span class="fl butn cut hd">-</span>
                                <span class="fl num">${num[j]}</span>
                                <span class="fl butn add">+</span>
                                <!-- 数量input -->
                                <input class="num-inp" type="hidden" value="1" id="pdt_num_2574" types="3"
                                    pdt_id="2574" stock="994" good_type="0" pdt_sale_price="${hsaledata[0][sid[j] - 1].price}">
                            </div>
                            <!--赠送积分-->
                            <div class="fl integral integral-pice" data-point="${hsaledata[0][sid[j] - 1].price}">${hsaledata[0][sid[j]].price}</div>
                            <div class="fl plan">¥<span class="unit-html sum-pice">${hsaledata[0][sid[j] - 1].price}</span></div>
                            <div class="fl dele" data-pid="2574">删除</div>
                        </div>
                    </li>
                        `;
                        price += Number(hsaledata[0][sid[j] - 1].price) * Number(num[j]);
                    } else if (datatype[j] = 'f1') {
                        strhtml += `
                        <li class="hover1">
                        <div class="top f-cb">
                            <div class="fl check checked" data-value="2574" data-name="pid[]" data-itemtype="" data-price="${f1data[0][sid[j] - 1].price}">
                                <input type="checkbox" value="2574" name="pid[]" checked="" itemtype="" data-price="">
                            </div>
                            <div class="fl intro">
                                <div class="pic">
                                     <img src="${f1data[0][sid[j] - 1].url}" alt="">
                                 </div>
                                 <h2>${f1data[0][sid[j] - 1].title}</h2>
                            </div>
                            <div class="fl price" data-unit="${f1data[0][sid[j] - 1].price}">¥<span class="unit-html">${f1data[0][sid[j] - 1].price}</span></div>
                            <div class="fl numbox" data-pdtid="2574" data-type="0">
                                <span class="fl butn cut hd">-</span>
                                <span class="fl num">${num[j]}</span>
                                <span class="fl butn add">+</span>
                                <!-- 数量input -->
                                <input class="num-inp" type="hidden" value="1" id="pdt_num_2574" types="3"
                                    pdt_id="2574" stock="994" good_type="0" pdt_sale_price="${f1data[0][sid[j] - 1].price}">
                            </div>
                            <!--赠送积分-->
                            <div class="fl integral integral-pice" data-point="${f1data[0][sid[j] - 1].price}">${f1data[0][sid[j]].price}</div>
                            <div class="fl plan">¥<span class="unit-html sum-pice">${f1data[0][sid[j] - 1].price}</span></div>
                            <div class="fl dele" data-pid="2574">删除</div>
                        </div>
                    </li>
                        `;
                        price += Number(f1data[0][sid[j] - 1].price) * Number(num[j]);
                    }
                }
                goodslist.html(strhtml);
                nun.html(sum);
                // select.html(sum);
                // allprice.html(price);

            });
            //单选框
            goodslist.delegate('input[type="checkbox"]', 'click', function () {
                let checkbox = $('.cart-list input[type="checkbox"]');
                let goodsnum = $('.cart-list .numbox .num');
                let goodsprice = $('.cart-list .price .simprice');
                let sallprice = $('.sum-pice');
                
            });
            //+-数量改变
            let arr1 = $.cookie('cookiesid').split(',');
            goodslist.delegate('li', 'click', function () {
                let li = $('.cart-list li');
                let dele = $(".cart-list .dele");
                let checkbox = $('.cart-list input[type="checkbox"]');
                let goodsnum = $('.cart-list .numbox .num');
                let add = $('.cart-list .numbox .add');
                let cut = $('.cart-list .numbox .cut');
                let goodsprice = $('.cart-list .price .simprice');
                let sallprice = $('.sum-pice');
                sum = 0;
                let arr = [];
                price = 0;
                let aum = 0;
                let p = 0;
                for (let i = 0; i < add.length; i++) {
                    sum += Number(goodsnum.eq(i).html());
                    let addprice = 0;
                    let startprice = Number(goodsnum.eq(i).html());
                    if (checkbox.eq(i).prop('checked')) {
                        price += Number(sallprice.eq(i).html());
                        aum += Number((goodsnum).eq(i).html());
                        p++;
                    }
                    else if (!checkbox.eq(i).prop('checked')) {
                        allcheck.prop('checked', false);
                    }
                    if (p > 2) {
                        allcheck.prop('checked', true);
                    }
                    add.eq(i).click(function () {
                        addprice++;
                        goodsnum.eq(i).html(startprice + addprice);
                        if (checkbox.eq(i).prop('checked')) {
                            price += Number(sallprice.eq(i).html());
                        }
                        if ((startprice + addprice) >= 150) {
                            goodsnum.eq(i).html(150);
                            add.eq(i).addClass('.hd');
                        }
                        if ((startprice + addprice) > 1) {
                            cut.eq(i).css({ color: '#4d4d4d' });
                        }
                    });
                    cut.eq(i).click(function () {
                        addprice--;
                        goodsnum.eq(i).html(startprice + addprice);
                        if (checkbox.eq(i).prop('checked')) {
                            price -= Number(sallprice.eq(i).html());
                        }
                        if ((startprice + addprice) <= 1) {
                            goodsnum.eq(i).html(1);
                            cut.eq(i).css({ color: '#e9e9e9' });
                        }
                        if ((startprice + addprice) < 150) {
                            add.eq(i).css({ color: '#4d4d4d' });
                        }
                    });
                    arr.push(goodsnum.eq(i).html());
                    dele.eq(i).click(function () {
                        li.eq(i).remove();
                        arr1.splice(i, 1);
                        arr.splice(i, 1);
                        datatype.splice(i, 1);
                        $.cookie('cookiesid', arr1.toString(), { expires: 7 });
                        $.cookie('cookienum', arr.toString(), { expires: 7 });
                        $.cookie('cookiedata', datatype.toString(), { expires: 7 });
                    });
                    sallprice.eq(i).html(Number(goodsprice.eq(i).html()) * goodsnum.eq(i).html());
                }
                select.html(aum);
                allprice.html(price);
                nun.html(sum);
            });
            //全选
            allcheck.click(function () {
                let checkbox = $('.cart-list input[type="checkbox"]');
                let goodsnum = $('.cart-list .numbox .num');
                let goodsprice = $('.cart-list .price .simprice');
                let sallprice = $('.sum-pice');
                price = 0;
                sum = 0
                if (allcheck.prop('checked')) {
                    checkbox.prop('checked', true);
                } else {
                    checkbox.prop('checked', false);
                }
                for (let i = 0; i < checkbox.length; i++) {
                    if (checkbox.eq(i).prop('checked')) {
                        price += Number(sallprice.eq(i).html());
                        sum += Number((goodsnum).eq(i).html());
                    }
                }
                select.html(sum);
                allprice.html(price);
            });
        });
    });
});